<template>
  <div class="register-box">
    <div class="title">
      <div class="left"  @click="toLogin">
        登录
      </div>
      <div class="middle">
        /
      </div>
      <div class="right">
        注册
      </div>
    </div>
    <div class="register-body">
      <el-form
          ref="registerRuleFormRef"
          :model="loginForm"
          :rules="registerRules"
          label-width="120px"
          class="register-form"
          status-icon
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="loginForm.name" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password"/>
        </el-form-item>
        <el-form-item label="确认密码" >
          <el-input v-model="loginForm.repassword" type="password"/>
        </el-form-item>
        <el-form-item class="submit">
          <el-button
              type="primary"
              @click="userRegister(registerRuleFormRef)"
              class="login-form-btn"
              size="large"
          >
            注册
          </el-button>
          <el-button
              @click="toLogin"
              class="login-form-btn"
              size="large"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {ElForm, ElFormItem, ElInput, ElButton, ElMessage} from "element-plus";
import {reactive, ref, toRefs} from "vue";
import {useRouter} from "vue-router";
import {register} from '@/api/index.js'
const router = useRouter()
const registerRuleFormRef = ref()
// 登录表单
let loginForm = reactive({
  name:'',
  password:'',
  repassword:'',
})
// 验证规则
// 检查两次密码是否一样
const {password,repassword} = toRefs(loginForm)
let validateCheckpwd = (rule, value, callback) => {
  if (value !== password.value) {
    callback(new Error('两次密码输入不一致'))
  }else {
    callback()
  }
}

const registerRules = reactive(
    {
      name: [
        {required: true, message: '用户名不能为空', trigger: 'blur'},
        {min: 2, max: 10, message: '长度必须为2-10个字符', trigger: 'blur'},
      ],
      password: [
        {required: true, message: '密码不能为空', trigger: 'blur'},
        {min: 6, max: 15, message: '长度必须为6-15个字符', trigger: 'blur'},
      ],
      sepassword: [
        {required: true, message: '密码不能为空', trigger: 'blur'},
        // {min: 6, max: 15, message: '长度必须为6-15个字符', trigger: 'blur'},
      ],
    }
)

const toLogin = ()=>{
  router.push('login')
}

//用户登录
const userRegister = async (formEl)=>{
  try {
    const v = await formEl.validate()
    if (v) {
      const {data:res} = await register({
        username:loginForm.name,
        password:loginForm.password,
        rePassword:loginForm.repassword,
      })

      //结果判断
      if(res.code === 200){
        ElMessage({
          message:'注册成功!',
          type:'success'
        })
        loginForm = {
          name:'',
          password:'',
          repassword:'',
        }
        router.push('login')
      } else if (res.code === 401) {
        ElMessage({
          message:'用户名已存在!',
          type:'info'
        })
        loginForm = {
          name:'',
          password:'',
          repassword:'',
        }
      } else {
        ElMessage({
          message:'注册失败!',
          type:'warning'
        })
        loginForm = {
          name:'',
          password:'',
          repassword:'',
        }
        return
      }
    } else {
      //数据验证未通过
      ElMessage({
        message:'请填写正确信息!',
        type:'error'
      })
    }
  } catch (e) {
    console.log(e)
  }
}
</script>

<style scoped lang="less">
.register-box{
  background-color: rgba(255,255,255,.4);
  position: absolute;
  left: 65%;
  top: 26%;
  border: solid 2px white;
  height: 50%;
  width: 25%;
  box-shadow: 0px 0px 25px #87CEFA;
  .title{
    height: 10%;
    display: flex;
    color: white;
    font-size: 3vh;
    text-align: left;
    margin-left: 2vw;
    margin-top: 3vh;
    align-items: baseline;
    letter-spacing: 6px;
    .left{
      margin-right: 0.5vw;
      font-size: 1.8vh;
      cursor: pointer;

      text-shadow: 0px 0px 2vw #1874CD;
      &:hover{
        text-decoration: underline;
        color: #0066ff;
      }
    }
    .right{
      margin-left: 0.5vw;
      text-shadow: 0px 0px 2vw #1874CD;
    }
  }
  .register-body{
    width: 90%;
    height: 55%;
    margin: 4vh auto;
    /deep/.el-form-item {
      display: flex;
      --font-size: 14px;
      margin-bottom: 3vh;
    }
    /deep/.el-form-item__label {
      justify-content: center;
      align-items: flex-start;
      font-size: 1.8vh !important;
      letter-spacing: 1px;
    }
    /deep/.el-input{
      width: 80%;
    }
    .submit{
      margin-top: 15%;
      justify-content: center;
      /deep/.el-form-item__content {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
        flex: 1;
        line-height: 32px;
        position: relative;
        font-size: var(--font-size);
        min-width: 0;
        margin-left: 0 !important;
      }
    }
    .login-form{}
  }
}
</style>